package cn.yuan.pay.service.impl;

import cn.yuan.basic.Exception.BusinessException;
import cn.yuan.basic.service.impl.BaseServiceImpl;
import cn.yuan.order.service.IProductOrderService;
import cn.yuan.pay.domain.AlipayInfo;
import cn.yuan.pay.domain.PayBill;
import cn.yuan.pay.mapper.AlipayInfoMapper;
import cn.yuan.pay.mapper.PayBillMapper;
import cn.yuan.pay.service.IPayBillService;
import cn.yuan.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill payBill) {
        //判断AdoptOrderServiceImpl传过来的payBill支付表单是否为空
        if(payBill==null){
            throw new BusinessException("你支付的支付单不存在，请创建支付单后再操作！");
        }
        //校验一下数据库的支付单是否存在
        PayBill payBill1 = payBillMapper.loadByUnionPaySn(payBill.getUnionPaySn());
        if(payBill1==null){
            throw new BusinessException("你支付的支付单不存在，请创建支付单后再操作！");
        }
        //根据支付类型完成支付
        Long payChannel = payBill.getPayChannel();//0 余额 1 支付宝 2 微信 3 银联
        switch (payChannel.intValue()){//转换成int类型
            case 1: //支付宝
                AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                return AlipayUtils.pay(alipayInfo, payBill);
            //微信
            case 2 :
                //@TODO
                break;
            //银联
            case 3 :
                //@TODO
                break;
            //余额
            default :
                //@TODO
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaysn) {
        return payBillMapper.loadByUnionPaySn(unionPaysn);
    }
}
